package com.polaris.live.utils.viewscope

import android.view.View
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.SupervisorJob

/**
 * autoDisposeScope
 * @author Created by 天晴 on 2024/1/10 9:10
 * @since 1.0.0
 **/
private val autoDisposeScopeKey = com.polaris.live.R.id.autodisposescope_key

val View.autoDisposeScope: CoroutineScope
    get() {
        val exist = getTag(autoDisposeScopeKey) as? CoroutineScope?
        if (exist != null) {
            return exist
        }
        val newScope =
            CoroutineScope(SupervisorJob() + Dispatchers.IO + ViewAutoDisposeInterceptor(this))
        setTag(autoDisposeScopeKey, newScope)
        return newScope
    }